package com.hjx.movie.core.system.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjx.movie.core.system.runner.SystemStaticData;
import com.hjx.movie.core.system.service.admin.ICinemaHallService;
import com.hjx.movie.persistence.system.mapper.IvyCinemaHallMapper;
import com.hjx.movie.persistence.system.model.IvyCinemaHall;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/9 - 14:50
 */
@Service
@Transactional
@Slf4j
@CacheConfig(cacheNames = "IvyMovie:SpringCache:CinemaHallCache",keyGenerator = "cacheKeyGenerator")
public class CinemaHallService extends ServiceImpl<IvyCinemaHallMapper, IvyCinemaHall> implements ICinemaHallService {
    @Resource
    private SystemStaticData staticData;

    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinemaHall> findAllCinemaHall() {
        return list();
    }

    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinemaHall> findAllCinemaHall(Wrapper<IvyCinemaHall> wrapper) {
        return list(wrapper);
    }

    @Cacheable(unless="#result == null")
    @Override
    public IvyCinemaHall findCinemaHallById(Long id) {
        return getById(id);
    }

    @Override
    public List<IvyCinemaHall> findCinemaHallByCondition(Long cinemaId, String cinemaHallNameLike, Integer cinemaHallStatus) {
        log.debug("条件查询影厅，接受到的入参为===>cinemaId:{},cinemaHallNameLike:{},cinemaHallStatus:{}",cinemaId,cinemaHallNameLike,cinemaHallStatus);
        QueryWrapper<IvyCinemaHall> wrapper = new QueryWrapper<>();
        if (cinemaId!=null){
            wrapper.eq("cinema_id",cinemaId);
        }
        if (StringUtils.isNotBlank(cinemaHallNameLike)){
            wrapper.like("cinema_hall_name",cinemaHallNameLike);
        }
        if (cinemaHallStatus!=null){
            wrapper.eq("status",cinemaHallStatus);
        }else {
            wrapper.ne("status",staticData.getStaticValue("STATUS_DELETE"));
        }
        return list(wrapper);
    }
    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean addCinemaHall(IvyCinemaHall cinemaHall) {
        log.debug("新增影厅");
        return save(cinemaHall);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean realDeleteCinemaHall(Long cinemaHallId) {
        log.debug("删除影厅");
        return removeById(cinemaHallId);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean updateCinemaHall(IvyCinemaHall cinemaHall) {
        log.debug("修改影厅信息");
        return updateById(cinemaHall);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean logicDeleteCinemaHall(Long cinemaHallId) {
        log.debug("逻辑删除影厅");
        return updateById(IvyCinemaHall.builder()
                .cinemaHallId(cinemaHallId)
                .status(Integer.parseInt(staticData.getStaticValue("STATUS_DELETE")))
                .build());
    }
}
